<template>
  <transition name="el-zoom-in-center">
    <div class="JADP-preview-main">
      <div class="JADP-common-page-header">
        <el-page-header
          @back="goBack"
          :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
        />
        <div class="options">
          <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail"
            >确 定</el-button
          >
          <el-button @click="goBack">取 消</el-button>
        </div>
      </div>
      <el-row
        :gutter="15"
        class="main"
        :style="{ margin: '0 auto', width: '100%' }"
      >
        <el-form
          ref="elForm"
          :model="dataForm"
          :rules="rules"
          size="small"
          label-width="100px"
          label-position="right"
          :disabled="!!isDetail"
        >
          <el-col :span="12">
            <el-form-item label="班级名称" prop="classname">
              <el-input
                v-model="dataForm.classname"
                placeholder="请输入"
                readonly
                @click.native.stop="classInquiry"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click.native.stop="classInquiry"
                  >查询</el-button
                >
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="学生姓名" prop="stuname">
              <el-input
                v-model="dataForm.stuname"
                placeholder="请输入"
                readonly
                @click.native.stop="studentInquiry"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click.native.stop="studentInquiry"
                  >查询</el-button
                >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考试类型" prop="testtype">
              <el-input
                v-model="dataForm.testtype"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
              <!-- <el-select
                v-model="dataForm.testtype"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                :multiple="false"
              >
                <el-option
                  v-for="(item, index) in getDict('shijuanleixing')"
                  :key="index"
                  :label="item.dataName"
                  :value="item.id"
                  :disabled="item.disabled"
                ></el-option>
              </el-select> -->
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="试卷名称" prop="papername">
              <el-input
                v-model="dataForm.papername"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="试卷总分" prop="papernumber">
              <el-input
                v-model.number="dataForm.papernumber"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="考试得分" prop="stuscore">
              <el-input
                v-model.number="dataForm.stuscore"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="线下学时" prop="offlinehours">
              <el-input
                v-model.number="dataForm.offlinehours"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>

          <!-- <el-col :span="12">
            <el-form-item label="考试开始时间" prop="starttime">
              <el-date-picker
                v-model="dataForm.starttime"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="timestamp"
              >
              </el-date-picker>
            </el-form-item>
          </el-col> -->

          <!-- <el-col :span="12">
            <el-form-item label="考试结束时间" prop="endtime">
              <el-date-picker
                v-model="dataForm.endtime"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="timestamp"
              >
              </el-date-picker>
            </el-form-item>
          </el-col> -->
        </el-form>
      </el-row>
      <ClazzTable
        ref="clazzTable"
        @currentRow="currentRow"
        v-show="classNamePage"
      />
      <CadetsTable
        ref="CadetsTable"
        @currentRow="currentRow2"
        v-show="studentNamePage"
      />
    </div>
  </transition>
</template>
<script>
import request from "@/utils/request";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import { previewDataInterface } from "@/api/systemData/dataInterface";
import ExportBox from "./ExportBox";
import ClazzTable from "./ClazzTable";
import CadetsTable from "./cadetsTable.vue";
export default {
  components: { ExportBox, ClazzTable, CadetsTable },
  props: [],
  data() {
    let checkStuscore = (rule, value, callback) => {
      if (value > this.dataForm.papernumber) {
        callback(new Error("考试得分不能大于试卷总分"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      visible: false,
      isDetail: false,
      activeName: "",
      dataForm: {
        classname: undefined,
        stuname: undefined,
        papername: undefined,
        stuscore: "",
        starttime: undefined,
        endtime: undefined,
        papernumber: "",
        testtype: undefined,
        offlinehours: "",
        organizeId: "",
      },
      rules: {
        classname: [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
          },
        ],
        stuname: [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
          },
        ],
        papername: [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
          },
        ],
        testtype: [
          {
            required: true,
            message: "试卷类型不能为空",
            trigger: "blur",
          },
        ],
        papernumber: [
          { required: true, message: "试卷总分不能为空", trigger: "blur" },
          { type: "number", message: "试卷总分必须为数字值", trigger: "blur" },
        ],
        stuscore: [
          {
            required: true,
            message: "考试得分不能为空",
            trigger: "blur",
          },
          {
            type: "number",
            message: "考试得分必须为数字值",
            trigger: "blur",
          },
          { validator: checkStuscore, trigger: "blur" },
        ],
        offlinehours: [
          {
            required: true,
            trigger: "blur",
            message: "线下学时不能为空",
          },
          {
            type: "number",
            message: "学时必须为数字值",
            trigger: "blur",
          },
        ],
      },
      classNamePage: false,
      studentNamePage: false,
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    goBack() {
      this.$emit("refresh");
    },
    // 班级查询
    classInquiry() {
      if (this.isDetail) return;
      this.classNamePage = true;
      // this.$refs.clazzTable.visible = true;
      this.$refs.clazzTable.initData();
    },
    // 学员查询
    studentInquiry() {
      if (this.isDetail) return;
      this.studentNamePage = true;
      // this.$refs.CadetsTable.visible = true;
      this.$refs.CadetsTable.initData();
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url:
              "/api/xy_xx_students_score/Xy_xx_students_score/" +
              this.dataForm.id +
              "?organizeId=" +
              this.$store.getters.organizeId,
            method: "get",
          }).then((res) => {
            this.dataForm = res.data;
            this.loading = false;
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.dataForm.organizeId = this.$store.getters.organizeId;
          if (!this.dataForm.id) {
            request({
              url: `/api/xy_xx_students_score/Xy_xx_students_score`,
              method: "post",
              data: this.dataForm,
            }).then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  (this.visible = false), this.$emit("refresh", true);
                },
              });
            });
          } else {
            request({
              url:
                "/api/xy_xx_students_score/Xy_xx_students_score/" +
                this.dataForm.id,
              method: "PUT",
              data: this.dataForm,
            }).then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refresh", true);
                },
              });
            });
          }
        }
      });
    },
    currentRow(val) {
      this.dataForm.classid = val.id;
      this.dataForm.classname = val.classname;
      this.classNamePage = false;
    },
    currentRow2(val) {
      this.dataForm.stuid = val.id;
      this.dataForm.stuname = val.realname;
      this.classNamePage = false;
    },
  },
};
</script>
